package com.demomaster.quickaccessibility.core;

import android.accessibilityservice.AccessibilityService;
import android.view.accessibility.AccessibilityEvent;

import com.demomaster.quickaccessibility.interfaces.OnAccessibilityEventListener;
import com.demomaster.quickaccessibility.interfaces.OnAccessibilityServiceConnectedListener;
import com.demomaster.quickaccessibility.plugin.IPlugin;
import com.demomaster.quickaccessibility.plugin.Plugin;
import com.demomaster.quickaccessibility.plugin.PluginManager;
import com.demomaster.quickaccessibility.service.AccessibilityAction;
import com.demomaster.quickaccessibility.service.EventFilter;
import com.demomaster.quickaccessibility.service.QuickAccessibilityService;

import java.util.HashMap;
import java.util.Map;

public class QuickAccessibility {
    private static QuickAccessibility instance;
    private final Map<String, EventFilter> eventFilters = new HashMap<>();
    public ServiceLifecycleNotifier serviceLifecycleNotifier;
    public boolean isScreenOn = true;
    //public AccessibilityEventProcessor mAccessibilityEventProcessor;
    public static QuickAccessibility getInstance() {
        if(instance==null){
            instance = new QuickAccessibility();
        }
        return instance;
    }
    
    private QuickAccessibility() {
        serviceLifecycleNotifier = new ServiceLifecycleNotifier(this);
        //mAccessibilityEventProcessor = new AccessibilityEventProcessor();
    }
    public void setScreenOn(boolean on)
    {
        isScreenOn = on;
    }
    AccessibilityService accessibilityService;

    public AccessibilityService getAccessibilityService() {
        return accessibilityService;
    }

    public void performGlobalAction(AccessibilityAction eventMessage){
        QuickAccessibilityService.instance.performGlobalAction(eventMessage);
    }

    public OnAccessibilityServiceConnectedListener mOnAccessibilityServiceConnectedListener;

    public void setOnAccessibilityServiceConnectedListener(OnAccessibilityServiceConnectedListener onAccessibilityServiceConnectedListener) {
        this.mOnAccessibilityServiceConnectedListener = onAccessibilityServiceConnectedListener;
    }

    public void registerEventListener(EventFilter filter, OnAccessibilityEventListener listener) {
        eventFilters.put(filter.getAction(), new EventFilter(filter, listener));
    }

    public void unregisterEventListener(String key) {
        eventFilters.remove(key);
    }

    public void registerPlugin(Plugin Plugin) {
        PluginManager.getInstance().registerPlugin(Plugin);
    }

    public void unregisterPlugin(Plugin Plugin) {
        PluginManager.getInstance().unregisterPlugin(Plugin);
    }

    public String getCurrentPackageName() {
        if(accessibilityService instanceof QuickAccessibilityService){
            return ((QuickAccessibilityService) accessibilityService).getCurrentPackageName();
        }
        return null;
    }

    public String getCurrentActivityName() {
        if(accessibilityService instanceof QuickAccessibilityService){
            return ((QuickAccessibilityService) accessibilityService).getCurrentActivityName();
        }
        return null;
    }

    public class ServiceLifecycleNotifier {
        QuickAccessibility accessibility;
        public ServiceLifecycleNotifier(QuickAccessibility quickAccessibility) {
            this.accessibility = quickAccessibility;
        }
        public void onServiceCreate(AccessibilityService instance) {
            accessibility.accessibilityService=instance;
            if(accessibility.mOnAccessibilityServiceConnectedListener!=null){
                accessibility.mOnAccessibilityServiceConnectedListener.onServiceCreate(instance);
            }
        }
        public void onServiceDestroy() {
            if(accessibility.mOnAccessibilityServiceConnectedListener!=null){
                accessibility.mOnAccessibilityServiceConnectedListener.onServiceDestroy();
            }
        }

        public void onAccessibilityEvent(AccessibilityEvent event) {
//            if(accessibility.mOnAccessibilityEventListener!=null){
//                accessibility.mOnAccessibilityEventListener.onAccessibilityEvent(quickAccessibilityEvent);
//            }
//
            PluginManager.getInstance().dispatchEvent(event);
//            for (Plugin plugin : PluginManager.getInstance().getPlugins().values()) {
//                plugin.onAccessibilityEvent(event,accessibility.getCurrentPackageName(),accessibility.getCurrentActivityName());
//            }
//            for (EventFilter eventFilter : eventFilters.values()) {
//                //if (eventFilter.filterEvent(event)) {
//                eventFilter.getListener().onAccessibilityEvent(event);
//                //}
//            }
        }

//        public void processEvent(AccessibilityService accessibilityService, android.view.accessibility.AccessibilityEvent event) {
//            accessibility.mAccessibilityEventProcessor.processEvent(accessibilityService,event);
//        }
    }
}
